//
//  preferViewController.m
//  Socrates
//
//  Created by shy on 15/3/8.
//  Copyright (c) 2015年 shy. All rights reserved.
//

#import "PreferViewController.h"

#import "PreferActivityViewController.h"
#import "PreferShopViewController.h"

static const CGFloat kSegmentedItemWidth = 72.f;

@interface PreferViewController ()
@property(strong, nonatomic) UISegmentedControl *segmentControl;
@property(weak, nonatomic) UIViewController *currentViewController;
@property(strong, nonatomic) NSArray *viewControllers;
//@property (strong, nonatomic) UIBarButtonItem *preferRightBarBtn;

@end

@implementation PreferViewController
- (void)dealloc {
    NSLog(@"%@ dealloc", self.class);
    //    [[NSNotificationCenter defaultCenter] removeObserver:self
    //                                                    name:ConversationUnreadCount
    //                                                  object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
    //    [[NSNotificationCenter defaultCenter] addObserver:self
    //                                             selector:@selector(conversationUnreadCount:)
    //                                                 name:ConversationUnreadCount
    //                                               object:nil];
    
    self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[
                                                                      NSLocalizedString(@"活动", nil),
                                                                      NSLocalizedString(@"商家", nil)
                                                                      ]];
    for (int i = 0; i < self.segmentControl.numberOfSegments; i++) {
        [self.segmentControl setWidth:kSegmentedItemWidth forSegmentAtIndex:i];
    }
    self.segmentControl.selectedSegmentIndex = 0;
    [self.segmentControl addTarget:self
                            action:@selector(segmentChanged:)
                  forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = self.segmentControl;
//    self.navigationItem.rightBarButtonItem = [self makeBarBtn];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self segmentChanged:self.segmentControl];
    //    [self updateUnreadChatCount:[ConversationCount instance].unreadCount];
    //    NSUInteger unreadNotificationsCount = [RSTNotification loadUnreadNotifications].count;
    //    if ([UserPreferences unreadNotificationCount] > 0) {
    //        // unread count in prefs doesn't merged by story, so only show an indicator
    //        [self showUnreadNotificationIndicator];
    //        [UserPreferences saveUnreadNotificationCount:0];  // should clear after loading data from server
    //    } else {
    //        [self updateUnreadNotificationsCount:unreadNotificationsCount];
    //    }
}

- (void)segmentChanged:(UISegmentedControl *)sender {
    [self.currentViewController.view removeFromSuperview];
    [self.currentViewController removeFromParentViewController];
    
    UIViewController *vc = [self viewControllerForSegmentIndex:sender.selectedSegmentIndex];
    vc.edgesForExtendedLayout = UIRectEdgeNone;
    CGRect frame = [UIScreen mainScreen].bounds;
    frame.size.height = CGRectGetHeight([UIScreen mainScreen].bounds) - 64;
    vc.view.frame = frame;
    [self addChildViewController:vc];
    [self.view addSubview:vc.view];
    [vc didMoveToParentViewController:self];
    
    self.currentViewController = vc;
    self.navigationItem.title = vc.title;
}

- (UIViewController *)viewControllerForSegmentIndex:(NSInteger)index {
    if (!self.viewControllers) {
        self.viewControllers = @[
                                 [[PreferActivityViewController alloc] init],
                                 [[PreferShopViewController alloc] init],
                                 ];
    }
    return self.viewControllers[index];
}

- (void)conversationUnreadCount:(NSNotification*)notification {
    NSUInteger unreadCount = [[[notification userInfo] objectForKey:@"unread"] unsignedIntValue];
    [self updateUnreadChatCount:unreadCount];
}

- (void)updateUnreadChatCount:(NSUInteger)count {
    NSString *title;
    if (count > 0) {
        title = [NSString stringWithFormat:@"%@ (%lu)", NSLocalizedString(@"活动", nil), (unsigned long)count];
    } else {
        title = NSLocalizedString(@"活动", nil);
    }
    [self.segmentControl setTitle:title forSegmentAtIndex:0];
}

- (void)updateUnreadNotificationsCount:(NSUInteger)count {
    NSString *title;
    if (count > 0) {
        title = [NSString stringWithFormat:@"%@ (%lu)", NSLocalizedString(@"商家", nil), (unsigned long)count];
    } else {
        title = NSLocalizedString(@"商家", nil);
    }
    [self.segmentControl setTitle:title forSegmentAtIndex:1];
}

- (void)showUnreadNotificationIndicator {
    [self.segmentControl setTitle:[NSString stringWithFormat:@"%@ ●", NSLocalizedString(@"商家", nil)] forSegmentAtIndex:1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (UIBarButtonItem *)makeBarBtn {
//    UIButton *settings = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 20)];
//    [settings setBackgroundImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
//    [settings addTarget:self action:@selector(clickRightBarButton:) forControlEvents:UIControlEventTouchUpInside];
//    self.preferRightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:settings];
//    
//    return self.preferRightBarBtn;
//}

- (void)clickRightBarButton:(id)sender {
    
}

@end
